﻿using OSSUtility.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.Device
{
    public class DevSOPASocaXY : DevBase
    {
        public DevSOPASocaXY(int CommType, string FSrvIP, int FPort) : base(CommType, FSrvIP, FPort)
        {

        }



        //RS485通信帧格式说明：
        //发送数据格式：
        //头码 地址 命令/数据 尾码 校验和
        //从机回应帧格式：
        //头码 地址 数据（固定9字节） 尾码 校验和
        //头码：终端调试头码为‘/’（0x2F）字符, OEM通信头码为‘[’（0x5B）字符，1个字节
        //地址：设备的节点地址，范围1~254， 255为广播地址，多字节ASCII（注意，地址不可为47，69，91）。
        //命令/数据：发送接收的数据或命令区域，多字节(具体命令，数据描述详见第5章节）
        //尾码：尾码为‘E’(0x45)字符，1字节
        //校验和：校验为以上数据的累加值，1字节；终端调试不关心
        //0x5B (91) + 0x32 (50) + 0x48 (72) + 0x45 (69) = 282（十进制）
        //282 % 256 = 26 → 0x1A




        /// <summary>
        /// 初始化ADP
        /// </summary>
        /// <returns></returns>
        public bool InitADP()
        {
            bool temperature = false;
            try
            {
                //前缀 头码和地址  2F不用加校验  5B着需要加校验
                byte[] bytesprefix = new byte[] { 0x2F, 0x32 };
                string parameter = "HE";
                byte[] bytesparameter = Encoding.ASCII.GetBytes(parameter);
                byte[] bytesSend = CombineByteArrays(bytesprefix, bytesparameter);
                //2F 32 52 45
                byte[] byteRcv = session_.sendAndReadByteLock(bytesSend.ToArray());
                if (byteRcv != null && byteRcv.Length == 26)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {
                temperature = false;
            }
            return temperature;

        }

        /// <summary>
        /// 退Tip头
        /// </summary>
        /// <returns></returns>
        public bool DropDitt()
        {

            bool temperature = false;
            try
            {
                //前缀 头码和地址  2F不用加校验  5B着需要加校验
                byte[] bytesprefix = new byte[] { 0x2F, 0x32 };
                string parameter = "RE";
                byte[] bytesparameter = Encoding.ASCII.GetBytes(parameter);
                byte[] bytesSend = CombineByteArrays(bytesprefix, bytesparameter);
                //2F 32 52 45
                byte[] byteRcv = session_.sendAndReadByteLock(bytesSend.ToArray());
                if (byteRcv != null && byteRcv.Length == 13)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {
                temperature = false;
            }
            return temperature;

        }


        /// <summary>
        /// 吸液
        /// </summary>
        /// <param name="aspirationVol">吸液量（单位：ul）</param>
        /// <param name="speed"> 吸液速度（单位：0.1ul/s）</param>
        public bool Aspirate(string aspirationVol, int speed)
        {

            bool temperature = false;
            try
            {
                //aspirationVol = "900";
                //前缀 头码和地址  2F不用加校验  5B着需要加校验
                byte[] bytesprefix = new byte[] { 0x2F, 0x32 };
                string parameter = "A" + aspirationVol + "E";
                byte[] bytesparameter = Encoding.ASCII.GetBytes(parameter);
                byte[] bytesSend = CombineByteArrays(bytesprefix, bytesparameter);
                //2F 32 41 31 30 30 30 45
                byte[] byteRcv = session_.sendAndReadByteLock(bytesSend.ToArray());
                if (byteRcv != null && byteRcv.Length == 26)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {
                temperature = false;
            }
            return temperature;

        }



        /// <summary>
        /// 吐液
        /// </summary>
        /// <param name="dispenseVol">吸液量（单位：ul）</param>
        public bool Dispense(float dispenseVol, int speed)
        {

            bool temperature = false;
            try
            {
                //前缀 头码和地址  2F不用加校验  5B着需要加校验
                byte[] bytesprefix = new byte[] { 0x2F, 0x32 };
                string parameter = "A" + dispenseVol + "E";
                byte[] bytesparameter = Encoding.ASCII.GetBytes(parameter);
                byte[] bytesSend = CombineByteArrays(bytesprefix, bytesparameter);
                //2F 32 41 30 30 30 30 45
                byte[] byteRcv = session_.sendAndReadByteLock(bytesSend.ToArray());
                if (byteRcv != null && byteRcv.Length == 26)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {
                temperature = false;
            }
            return temperature;

        }


        /// <summary>
        /// 检测吸头是否存在
        /// </summary>
        /// <returns></returns>
        public bool DetectionTip()
        {
            bool temperature = false;
            try
            {
                //前缀 头码和地址  2F不用加校验  5B着需要加校验
                byte[] bytesprefix = new byte[] { 0x2F, 0x32 };
                string parameter = "Q28E";
                //Q28   ASCII码转换成16进制
                byte[] bytesparameter = Encoding.ASCII.GetBytes(parameter);
                byte[] bytesSend = CombineByteArrays(bytesprefix, bytesparameter);
                //2F 32 51 32 38 45
                byte[] byteRcv = session_.sendAndReadByteLock(bytesSend.ToArray());
                if (byteRcv != null && byteRcv.Length == 26)
                {
                    if (byteRcv[16] == 0X30)
                    {
                        temperature = false;

                    }
                    if (byteRcv[16] == 0X31)
                    {

                        temperature = true;
                    }
                }
            }
            catch (Exception ex)
            {
                temperature = false;
            }
            return temperature;

        }




        public static byte[] CombineByteArrays(byte[] first, byte[] second)
        {
            if (first == null) first = Array.Empty<byte>();
            if (second == null) second = Array.Empty<byte>();

            byte[] result = new byte[first.Length + second.Length];
            Array.Copy(first, 0, result, 0, first.Length);
            Array.Copy(second, 0, result, first.Length, second.Length);
            return result;
        }


    }
}
